import {
    reqRadioCategoryIconList,
    getDjRadioRecommend,
    getDjRadios
} from "@/api/djradio";
import { categoryIconList } from "@/api/local-data";
import * as actionTypes from "./constants";

const changeCategoryAction = res => ({
    type: actionTypes.CHANGE_RADIO_CATEGORY,
    categories: res.categories.concat(categoryIconList)
});

const changeRecommendsAction = res => ({
    type: actionTypes.CHANGE_RECOMMENDS,
    recommends: res.djRadios
});

const changeRadiosAction = res => ({
    type: actionTypes.CHANGE_RADIOS,
    radios: res.djRadios
});

export const changeCurrentIdAction = id => ({
    type: actionTypes.CHANGE_CURRENT_ID,
    currentId: id
});

export const getCategoryIconListAction = () => {
    return dispatch => {
        reqRadioCategoryIconList().then(res => {
            dispatch(changeCategoryAction(res));
            const currentId = res.categories[0].id;
            dispatch(changeCurrentIdAction(currentId));
        });
    };
};

export const getRadioRecommendAction = currentId => {
    return dispatch => {
        getDjRadioRecommend(currentId).then(res => {
            dispatch(changeRecommendsAction(res));
        });
    };
};

export const getRadioAction = (currentId, offset) => {
    return dispatch => {
        getDjRadios(currentId, 30, offset).then(res => {
            dispatch(changeRadiosAction(res));
        });
    };
};